import { message } from 'antd';
import { getDistrictListByBussiness, addBussiness, getBussinessList, getBussinessDetail } from '../services/api';

export default {
  namespace: 'bussiness',

  state: {
    data: {
      list: [],
      pagination: {},
    },
    bussinessList: [],
    districtList: [],
    networkSpeedList: [],
    bussinessInfo: {},
  },

  effects: {
    *fetch({ payload }, { call, put }) {
      const response = yield call(getBussinessList, payload);
      yield put({
        type: 'bussinessList',
        payload: response,
      });
    },
    *add({ payload, callback }, { call, put }) {
      const response = yield call(addBussiness, payload);
      if (response.code === 0) {
        message.success('新增成功');
        if (callback) callback();
      } else {
        message.error('新增失败');
      }
    },
    *districtList({ payload }, { call, put }) {
      const response = yield call(getDistrictListByBussiness, payload);
      yield put({
        type: 'districtList',
        payload: response,
      });
    },
    *networkSpeedList({ payload }, { call, put }) {
      const response = yield call(getBussinessList, payload);
      yield put({
        type: 'networkSpeedList',
        payload: response,
      });
    },
    *info({ payload }, { call, put }) {
      const response = yield call(getBussinessDetail, payload);
      yield put({
        type: 'saveBussinessInfo',
        payload: response,
      });
    },
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        data: action.payload,
      };
    },
    districtList(state, action) {
      return {
        ...state,
        districtList: action.payload.data,
      };
    },
    bussinessList(state, action) {
      return {
        ...state,
        bussinessList: action.payload.data,
      };
    },
    networkSpeedList(state, action) {
      return {
        ...state,
        networkSpeedList: action.payload.data,
      };
    },
    saveBussinessInfo(state, action) {
      return {
        ...state,
        bussinessInfo: action.payload.data,
      };
    },
  },
};
